/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pantallas;

import clases.GestorGrabarLeerDelDisco;
import clases.Herramienta;
import clases.HerramientaConCantidad;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author DanielRH
 */
public class PantallaAgregarHerramienta extends javax.swing.JDialog {

    /**
     * Creates new form PantallaAgregarHerramienta
     */
    public PantallaAgregarHerramienta(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        cargarTabla();
        cargarMouseListener();
        txtCantidad.addKeyListener(new KeyAdapter()
        {
            @Override
        public void keyTyped(KeyEvent e)
        {
            char caracter = e.getKeyChar();
            // Verificar si la tecla pulsada no es un digito
            if(((caracter < '0') ||
                (caracter > '9')) &&
                (caracter != '\b' /*corresponde a BACK_SPACE*/))
            {
                e.consume();  // ignorar el evento de teclado
            }
        }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tablaHerramientas = new javax.swing.JTable();
        btnNueva = new javax.swing.JButton();
        txtNuevaHerramienta = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnAceptar = new javax.swing.JButton();
        txtSeleccionada = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        tablaHerramientas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tablaHerramientas);

        btnNueva.setText("Agregar");
        btnNueva.setEnabled(false);

        txtNuevaHerramienta.setEnabled(false);

        jLabel1.setText("Nueva Herramienta:");

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        txtSeleccionada.setEditable(false);

        jLabel2.setText("Seleccionada:");

        jLabel3.setText("Cantidad:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNuevaHerramienta)
                        .addGap(18, 18, 18)
                        .addComponent(btnNueva)
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtCantidad)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnAceptar))
                            .addComponent(txtSeleccionada))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNuevaHerramienta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(btnNueva))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSeleccionada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(btnAceptar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:
        if(txtSeleccionada.getText().compareTo("")!=0&&txtCantidad.getText().compareTo("")!=0)
        {
            PantallaAgregarTrabajo padre=(PantallaAgregarTrabajo) this.getParent();
            HerramientaConCantidad nuevoCant=new HerramientaConCantidad();
            nuevoCant.setActual(new Herramienta(txtSeleccionada.getText()));
            nuevoCant.setCantidad(Integer.parseInt(txtCantidad.getText()));
            padre.agregarNuevaHerramienta(nuevoCant);
        }
        this.dispose();
    }//GEN-LAST:event_btnAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaAgregarHerramienta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaAgregarHerramienta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaAgregarHerramienta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaAgregarHerramienta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                PantallaAgregarHerramienta dialog = new PantallaAgregarHerramienta(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    private void cargarTabla()
    {
        GestorGrabarLeerDelDisco manejador=new GestorGrabarLeerDelDisco();
        LinkedList listaHerramientas=manejador.obtenerLista(new Herramienta());
        String nombresColumnas[] = {"Herramientas"};
        herramientasTablaModel = new DefaultTableModel(nombresColumnas,0);
        Iterator iter = listaHerramientas.iterator();
        while(iter.hasNext())
        {
            Herramienta aux = (Herramienta)iter.next();
            String datosFila[] = {aux.getNombre()};
            herramientasTablaModel.addRow(datosFila);
            
        }
        tablaHerramientas.setModel(herramientasTablaModel);
    }
    
    private void cargarMouseListener()
    {
        MouseAdapter adaptadorMouse=new MouseAdapter()
           {@Override
           public void mouseClicked(MouseEvent e) 
              {
                 int fila = tablaHerramientas.rowAtPoint(e.getPoint());
                 int columna = tablaHerramientas.columnAtPoint(e.getPoint());
                 if ((fila > -1) && (columna > -1))
                 {
                     txtSeleccionada.setText(tablaHerramientas.getModel().getValueAt(fila, 0)+"");
//                     herramientaSeleccionada=tablaHerramientas.getModel().getValueAt(fila, 0)+"";
//                     pacienteSeleccionado.setDni(Integer.parseInt(tablaEstructuras.getModel().getValueAt(fila, 0)+""));
//                     pacienteSeleccionado.setApellidoNombre(tablaEstructuras.getModel().getValueAt(fila, 1)+"");
                 }
              }
           };
        
        tablaHerramientas.addMouseListener(adaptadorMouse);
    }
    
    DefaultTableModel herramientasTablaModel;
    private Herramienta herramientaSeleccionada;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnNueva;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaHerramientas;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtNuevaHerramienta;
    private javax.swing.JTextField txtSeleccionada;
    // End of variables declaration//GEN-END:variables
}
